<template>
  <div id="app" >
      <el-input v-model="question" placeholder="请输入问题" style="width:300px"></el-input>
    <el-button @click="getIssueList" type="primary" size="small">查询</el-button>&nbsp;&nbsp;&nbsp;&nbsp;
     <el-button @click="insertIssue" type="primary" size="small">添加</el-button>
     <!-- <el-button type="primary">上传<i class="el-icon-upload el-icon--right"></i></el-button> -->
    <el-table :data="tableData" border style="width: 71%">
      <el-table-column fixed prop="id" label="问题ID" width="120"></el-table-column>
      <el-table-column prop="question" label="问题内容" width="120"></el-table-column>
      <el-table-column prop="answer" label="问题回复" width="400"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
           <el-button @click="upd(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="delClick(scope.row.id)" type="danger" size="small">删除</el-button> 
        </template>
      </el-table-column>
       <el-table-column fixed="right" label="移动" width="100">
        <template slot-scope="scope">
          <!-- <el-button @click="shangClick(scope.row)" type="warning" round size="small">上移</el-button> -->
        <el-button @click="xiaClick(scope.row)" type="warning" round  size="small">下移</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  

      <!-- 添加对话框 -->
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
  <el-form :model="form" :rules="rules">
    <el-form-item label="问题" :label-width="formLabelWidth" prop="question">
      <el-input v-model="form.question" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="回复" :label-width="formLabelWidth" prop="answer">
      <el-input v-model="form.answer"  autocomplete="off" width="300" ></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addObject()">确 定</el-button>
  </div>
</el-dialog>

  <!-- 修改对话框 -->
    <el-dialog title="编辑" :visible.sync="updFlag">
  <el-form :model="form" :rules="rules">
      <el-input v-model="form.id" type="hidden" autocomplete="off"></el-input>
    <el-form-item label="问题" :label-width="formLabelWidth" prop="question">
      <el-input v-model="form.question" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="回复" :label-width="formLabelWidth" prop="answer">
      <el-input v-model="form.answer" autocomplete="off" ></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="updFlag = false">取 消</el-button>
    <el-button type="primary" @click="UpdObject()">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
export default {
  mounted() {
    this.getIssueList();
  },
  methods: {
    xiaClick(row){
      var $this=this;
      this.axios({
        method:'PUT',
        url: "http://localhost:8081/ecIssue/xiayidong",
        params:{id:row.id}
      }).then(function(res){
          $this.getIssueList();
        })
    },
    shangClick(row){
      var $this=this;
      this.axios({
        method:'PUT',
        url: "http://localhost:8081/ecIssue/yidong",
        params:{id:row.id}
      }).then(function(res){
          $this.getIssueList();
        })
    },
    addObject(){
      var $this=this;
      this.axios({
        method: "post",
        url: "http://localhost:8081/ecIssue/xujiabao/issueAdd",
        data:this.form
      }).then(function(res) {
        if(res.data.code==200){
            alert("成功");
            $this.getIssueList();
            $this.dialogFormVisible=false;
        }else{
            alert("失败");
        }
      }); 
    },
    insertIssue(){
        this.dialogFormVisible=true;
        for(var i in this.form){
        this.form[i]=null;
      }
    },
    UpdObject(){
      var $this=this;
      this.axios({
        method: "post",
        url:"http://localhost:8081/ecIssue/xujiabao/issueUpd",
        data:this.form
      }).then(function(res) {
        if(res.data.code==200){
            alert("成功");
            $this.getIssueList();
            $this.updFlag=false;
        }else{
            alert("失败");
        }
      }); 
    },
    upd(row){
      this.updFlag=true;
      this.form.id=row.id;
      this.form.question=row.question;
      this.form.answer=row.answer;
    },
    getIssueList() {
      var $this=this;
      this.axios({
        method: "GET",
        url: "http://localhost:8081/ecIssue/xujiabao/issueList",
        params: {
          pageNo: this.currentPage4,
          pageSize: this.pageSize,
           question:this.question,
        }
      }).then(function(res) {
        $this.tableData = res.data.obj.list;
        $this.total=res.data.obj.total;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getIssueList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage4 = val;
      this.getIssueList();
    },
    delClick(row){
      var $this=this;
      this.axios({
        method:'post',
        url: "http://localhost:8081/ecIssue/xujiabao/logicdel?id="+row,
      }).then(function(res){
          if(res.data.code==200){
              alert("删除成功");
              $this.getIssueList();
          }else{
            alert("删除有误");
          }
      })
    },
  },
  data() {
    return {
      msg: "Welcome to Your Vue.js App",
      currentPage4: 1,
      pageSize: 1,
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      updFlag:false,
      form: {
      },
      rules: {},
      formLabelWidth: "120px",
      question:'',
      rules: {
        question: [
          { required: true, trigger: "blur" ,message: "问题不能为空"},
          // { min: 1, max: 10, trigger: "blur" }
        ],
        answer: [
          { required: true, trigger: "blur" ,message: "回复不能为空"},
          // { min: 1, max: 10, trigger: "blur" }
        ]
      }
    };
  }
};
</script>

